package com.hui.platform.falseworkapi.common.core.exception;

import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * 基础异常抛出接口
 * 条件成立则抛出异常
 *
 * @author Peng
 * @date 2019/11/4
 */
public interface IThrow {
    /**
     * 自定义异常
     *
     * @return 自定义异常
     */
    BaseException newException();

    BaseException newException(Throwable cause);

    //--------------------------断言方法区-----------------------

    default void throwIfTrue(boolean expression) {
        if (expression) {
            throw newException();
        }
    }

    default void throwIfFalse(boolean expression) {
        if (!expression) {
            throw newException();
        }
    }

    default void throwIfNull(Object obj) {
        if (obj == null) {
            throw newException();
        }
    }

    default void throwIfNotNull(Object obj) {
        if (obj != null) {
            throw newException();
        }
    }

    default void throwIfEmpty(String str) {
        if (null == str || "".equals(str.trim())) {
            throw newException();
        }
    }

    default void throwIfEmpty(Object[] arrays) {
        if (arrays == null || arrays.length == 0) {
            throw newException();
        }
    }

    default void throwIfEmpty(Collection<?> c) {
        if (c == null || c.isEmpty()) {
            throw newException();
        }
    }

    default void throwIfNotEmpty(Collection<?> c) {
        if (c != null && c.size() > 0) {
            throw newException();
        }
    }

    default void throwIfEmpty(Map<?, ?> map) {
        if (map == null || map.isEmpty()) {
            throw newException();
        }
    }

    /**
     * 直接抛出异常
     */
    default void throwException() {
        throw newException();
    }

    default void throwException(Throwable cause) {
        throw newException(cause);
    }

    /**
     *  如果对象不相等，抛出异常
     *
     * @param o1 对象1
     * @param o2 对象2
     */
    default void throwIfNotEquals(Object o1, Object o2) {
        if (o1 == o2) {
            return;
        }
        if (o1 == null) {
            throw newException();
        }
        if (!o1.equals(o2)) {
            throw newException();
        }
    }

    default void throwIfNoLength(String text) {
        if (!StringUtils.hasLength(text)) {
            throw newException();
        }
    }

    default void throwIfNoText(String text) {
        if (!StringUtils.hasText(text)) {
            throw newException();
        }
    }
    default void throwIfContain(String textToSearch, String substring) {
        if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
                textToSearch.contains(substring)) {
            throw newException();
        }
    }

    default void throwIfHasNullElement(Object[] array) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    throw newException();
                }
            }
        }
    }

    default void throwIfHasNullElement(Collection<?> collection) {
        if (collection != null) {
            for (Object element : collection) {
                if (element == null) {
                    throw newException();
                }
            }
        }
    }

}
